package com.example.textswitchtestdemo01;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.textswitchtestdemo01.activity.EighthActivity;
import com.example.textswitchtestdemo01.activity.FifthActivity;
import com.example.textswitchtestdemo01.activity.FirstActivity;
import com.example.textswitchtestdemo01.activity.FourthActivity;
import com.example.textswitchtestdemo01.activity.SeventhActivity;
import com.example.textswitchtestdemo01.activity.SixthActivity;
import com.example.textswitchtestdemo01.activity.ThirdActivity;
import com.example.textswitchtestdemo01.util.InitToolbar;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements TextSwitcher.ViewFactory, View.OnClickListener {

    private TextSwitcher ts_main;
    private String[] strings = {"第一行", "第二行", "第三行", "第四行", "第五行"};
    private int num = 0;
    private SharedPreferences mode;

    //主题切换
    private int mTheme = 0;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                Log.d("MainActivity", "string:" + strings[num]);
                ts_main.setText(strings[num]);
                if (num != strings.length - 1) {
                    num++;
                } else {
                    num = 0;
                }
            }
        }
    };
    private Timer timer;
    private Spinner sp_main;
    private Button btn_go;
    private Button btn_see;
    private Button mainBTN;
    private Button btn_change;
    private TextView tv_mode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mode = getSharedPreferences("Setting", MODE_PRIVATE);
        mTheme = mode.getInt("Mode", 0);
        if (mTheme != 0) {
            setTheme(mTheme);
        }

        setContentView(R.layout.activity_main);
        InitToolbar.initToolbar(this, "主页", false);

        initView();

        initTimer();

    }

    private void initTimer() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(0);
            }
        }, 100, 3000);
    }

    private void initView() {

        ts_main = (TextSwitcher) findViewById(R.id.ts_main);
        ts_main.setFactory(this);
        ts_main.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.ts_inanim));
        ts_main.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.ts_outanim));
        sp_main = (Spinner) findViewById(R.id.sp_main);
        sp_main.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, final View view, final int position, long id) {
                btn_go.setOnClickListener(new View.OnClickListener() {

                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

                    @Override
                    public void onClick(View v) {
                        switch (position) {
                            case 0:
                                startActivity(new Intent(MainActivity.this, FirstActivity.class));
                                break;
                            case 2:
                                startActivity(new Intent(MainActivity.this, ThirdActivity.class));
                                break;
                            case 3:
                                startActivity(new Intent(MainActivity.this, FourthActivity.class));
                                break;
                            case 4:
                                startActivity(new Intent(MainActivity.this, FifthActivity.class));
                                break;
                            case 5:
                                startActivity(new Intent(MainActivity.this, SixthActivity.class));
                                break;
                            case 6:
                                startActivity(new Intent(MainActivity.this, SeventhActivity.class));
                                break;
                            case 7:
                                startActivity(new Intent(MainActivity.this, EighthActivity.class));
                                break;
                            default:
                                builder.setTitle("你点击了第" + (position + 1) + "项");
                                builder.setMessage(sp_main.getSelectedItem().toString());
                                builder.show();
                                break;
                        }
                    }
                });
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        btn_go = (Button) findViewById(R.id.btn_go);
        btn_see = (Button) findViewById(R.id.btn_see);
        btn_see.setOnClickListener(this);
        btn_change = (Button) findViewById(R.id.btn_change);
        btn_change.setOnClickListener(this);
        tv_mode = (TextView) findViewById(R.id.tv_mode);

        if (mTheme == R.style.ThemeOverlay_AppCompat_Dark_ActionBar) {
            tv_mode.setText("夜间");
        } else {
            tv_mode.setText("日间");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }

    @Override
    public View makeView() {
        TextView tv = new TextView(this);
        tv.setGravity(Gravity.CENTER);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        layoutParams.gravity = Gravity.CENTER;
        tv.setLayoutParams(layoutParams);
        return tv;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_see:

                createPopwindow();

                List<String> list = listRaw();
                for (String s : list) {
                    int raw = getResources().getIdentifier(s, "raw", getPackageName());
                    Log.d("mainActivity", "id = " + raw);
                    Log.d("mainActivity", "id22 = " + R.raw.aa);
                }
                break;
            case R.id.btn_change:

                if (mTheme == R.style.ThemeOverlay_AppCompat_Dark_ActionBar) {
                    mTheme = R.style.ThemeOverlay_AppCompat;
                    tv_mode.setText("日间");
                } else {
                    mTheme = R.style.ThemeOverlay_AppCompat_Dark_ActionBar;
                    tv_mode.setText("夜间");
                }
                SharedPreferences.Editor edit = mode.edit();
                edit.putInt("Mode", mTheme);
                edit.apply();
                recreate();

                break;
        }
    }

    private void createPopwindow() {
        final PopupWindow popupWindow = new PopupWindow(getApplicationContext());
        View inflate = LayoutInflater.from(getApplicationContext()).inflate(R.layout.pw_content, null);
        popupWindow.setContentView(inflate);
        //        popupWindow.showAsDropDown(btn_see);

        mainBTN = inflate.findViewById(R.id.btn_main);
        popupWindow.showAsDropDown(btn_see, 0, -200, Gravity.END);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        mainBTN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                Toast.makeText(getApplicationContext(), "弹出", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public List<String> listRaw() {
        List<String> list = new ArrayList<>();
        Field[] fields = R.raw.class.getFields();
        for (int i = 0; i < fields.length; i++) {
            String name = fields[i].getName();
            list.add(name);
            Log.d("mainActivity", name);
        }
        return list;
    }

    public void getAss() {

        String path = "file:///android_asset/sdfagdafga.mp3";
    }

}
